﻿#region Copyright

// **********************************************************************
// Script Name :		RedPointMgr.cs
// Author Name :		Your Name
// Create Time :		2024-06-07 18:03:06
// Description :
// **********************************************************************

#endregion

using System.Collections.Generic;
using UnityEngine;

namespace Jerry.UiFrame
{
    /// <summary>
    ///     红点管理系统
    /// </summary>
    public class RedPointMgr
    {
        private static RedPointMgr _instance;

        public static RedPointMgr Instance = _instance ?? new RedPointMgr();

        private Dictionary<string, RedPointNode> _nodes;

        private RedPointNode _root; //整个红点树的根节点

        private RedPointMgr()
        {
        }

        /// <summary>
        ///从RedPoints中初始化红点树
        /// </summary>
        public void Init()
        {
            _root = new RedPointNode(RedPoints.Root);
            _root.parent = null;
            _nodes = new Dictionary<string, RedPointNode>();
            _nodes.Add(_root.name, _root);

            foreach (var item in RedPoints.TreeNodes) CreateNode(item);
        }

        /// <summary>
        ///     创建红点树节点
        ///     ///
        /// </summary>
        /// <param name="nodeName"></param>
        public RedPointNode CreateNode(string nodeName)
        {
            var node = new RedPointNode(nodeName);
            if (!_nodes.ContainsKey(nodeName)) _nodes.Add(node.name, node);
            //查找父节点是否存在
            var index = nodeName.LastIndexOf('_');
            // var tempStr = nodeName.Substring(index);
            var parentName = nodeName.Substring(0, index);
            if (!_nodes.ContainsKey(parentName))
            {
                var parent = CreateNode(parentName);
                node.parent = parent;
                if (!node.parent.children.ContainsKey(node.name)) node.parent.children.Add(node.name, node);
            }
            else
            {
                node.parent = _nodes[parentName];
                if (!node.parent.children.ContainsKey(node.name)) node.parent.children.Add(node.name, node);
            }

            return node;
        }

        /// <summary>
        ///     设置红点个数
        /// </summary>
        /// <param name="pointName"></param>
        /// <param name="num"></param>
        public void SetPointCnt(string pointName, int num)
        {
            if (_nodes.ContainsKey(pointName))
            {
                if (_nodes[pointName].children.Count > 0)
                {
                    Debug.LogError($"节点--{pointName}--不是叶子节点，不能设置红点个数");
                }
                else
                {
                    var node = _nodes[pointName];
                    node.UpdateRedPointCnt(num);
                    if (node.redPointCnt == -1) return;
                    while (node.parent != null) //更新父节点红点数
                    {
                        node.parent.UpdateRedPointCnt(num);
                        node = node.parent;
                    }

                    EventMgr.Broadcast(EventEnum.RedPointUpdate);
                }
            }
        }

        /// <summary>
        ///     获取某个节点的红点数
        /// </summary>
        /// <param name="pointName"></param>
        /// <returns></returns>
        public int GetRedPointCnt(string pointName)
        {
            if (!_nodes.ContainsKey(pointName))
            {
                Debug.LogError($"不存在节点---{pointName}");
                return -1;
            }

            return _nodes[pointName].redPointCnt;
        }
    }
}